package cc.chinagps.gateway.util;

import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.TimeZone;

public class DateTimeUtil {
    public static final String DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final String DATETIME_FORMAT_SIMPLE = "yyMMddHHmmss";

    public static final String DATE_FORMAT_SIMPLE = "yyMMdd";

    public static final String DATETIME_FORMAT_FULL = "yyyyMMddHHmmss";

    public static LocalDateTime parseStringToDateTime(String time, String format) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
        return LocalDateTime.parse(time, df);
    }

    public static long getTimestampOfDateTimeUTC(LocalDateTime localDateTime) {
        ZoneId zone = TimeZone.getTimeZone("GMT").toZoneId();
        ;//beijing is GMT+8,UTC is GMT
        Instant instant = localDateTime.atZone(zone).toInstant();
        return instant.toEpochMilli();
    }

    public static long getTimestampOfDateTimeGMT8(LocalDateTime localDateTime) {
        ZoneId zone = TimeZone.getTimeZone("GMT+8").toZoneId();
        Instant instant = localDateTime.atZone(zone).toInstant();
        return instant.toEpochMilli();
    }

    public static LocalDateTime getDateTimeOfTimestampUTC(long timestamp) {
        Instant instant = Instant.ofEpochMilli(timestamp);
        ZoneId zone = TimeZone.getTimeZone("GMT").toZoneId();
        return LocalDateTime.ofInstant(instant, zone);
    }

    public static String getDateTimeAsString(LocalDateTime localDateTime, String format) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
        return localDateTime.format(formatter);
    }

    public static String getDateTimeAsStringGMT8(long timestamp, String format) {
        Instant instant = Instant.ofEpochMilli(timestamp);
        ZoneId zone = TimeZone.getTimeZone("GMT+8").toZoneId();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
        return localDateTime.format(formatter);
    }

    public static Timestamp toDBTimestamp(String dateTime, String format) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
        return Timestamp.valueOf(LocalDateTime.parse(dateTime, df));
    }

    public static byte[] getCurrentTimeBCD() {
        Instant instant = Instant.now();
        ZoneId zoneId = TimeZone.getTimeZone("GMT").toZoneId();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zoneId);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATETIME_FORMAT_SIMPLE);
        String s = localDateTime.format(formatter);
        return ConvertUtil.str2bcd(s);
    }

    public static byte[] getCurrentTimeBCD8() {
        Instant instant = Instant.now();
        ZoneId zoneId = TimeZone.getTimeZone("GMT+8").toZoneId();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zoneId);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATETIME_FORMAT_SIMPLE);
        String s = localDateTime.format(formatter);
        return ConvertUtil.str2bcd(s);
    }

    public static byte[] getCurrentDateBCD8() {
        Instant instant = Instant.now();
        ZoneId zoneId = TimeZone.getTimeZone("GMT+8").toZoneId();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zoneId);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATE_FORMAT_SIMPLE);
        String s = localDateTime.format(formatter);
        return ConvertUtil.str2bcd(s);
    }

    public static String getCurrentTimeGTM8(String pattern) {
        Instant instant = Instant.now();
        ZoneId zoneId = TimeZone.getTimeZone("GMT+8").toZoneId();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zoneId);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        String s = localDateTime.format(formatter);
        return s;
    }

    public static String getCurrentTimeUTC(String pattern) {
        Instant instant = Instant.now();
        ZoneId zoneId = TimeZone.getTimeZone("GMT").toZoneId();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zoneId);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        String s = localDateTime.format(formatter);
        return s;
    }

    public static String getDateTime(byte[] data, int offset, int len) {
        if (len != 6)
            return null;
        if (data.length < offset + len)
            return null;
        String s = "";
        for (int i = 0; i < len; i++) {
            s += getString(data[offset + i]);
        }

        return s;
    }

    public static byte[] getCurrentDateGTM8() {
        Instant instant = Instant.now();
        ZoneId zoneId = TimeZone.getTimeZone("GMT+8").toZoneId();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zoneId);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATE_FORMAT_SIMPLE);
        String s = localDateTime.format(formatter);
        return str2DateTime(s);
    }

    public static byte[] str2DateTime(String s) {
        int length = s.length();
        byte[] dateTime = new byte[length / 2];
        for (int i = 0; i < dateTime.length; i++) {
            dateTime[i] = Byte.valueOf(s.substring(i * 2, i * 2 + 2));
        }
        return dateTime;
    }

    //231123
    public static byte[] str2Bcd(String s) {
        int length = s.length();
        byte[] bcd = new byte[length / 2];
        for (int i = 0; i < bcd.length; i++) {
            byte b1 = Byte.valueOf(s.substring(i * 2, i * 2 + 1));
            byte b2 = Byte.valueOf(s.substring(i * 2 + 1, i * 2 + 2));
            bcd[i] = (byte) ((b1 << 4) | (b2 & 0xFF));
        }
        return bcd;
    }

    public static byte[] getCurrentDateTimeGTM8() {
        Instant instant = Instant.now();
        ZoneId zoneId = TimeZone.getTimeZone("GMT+8").toZoneId();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zoneId);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATETIME_FORMAT_SIMPLE);
        String s = localDateTime.format(formatter);
        return str2DateTime(s);
    }

    private static String getString(int i) {
        String str = "";
        if (i < 10)
            str = "0" + i;
        else {
            str = "" + i;
        }
        return str;
    }

    public static void main(String[] args) {
        System.out.println(getTimestampOfDateTimeUTC(LocalDateTime.now()));
        System.out.println(getTimestampOfDateTimeGMT8(LocalDateTime.now()));
        System.out.println(LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli());

        System.out.println(getDateTimeAsStringGMT8(System.currentTimeMillis(), DATETIME_FORMAT_SIMPLE));
        byte[] bytes = getCurrentDateTimeGTM8();
        System.out.println(Arrays.toString(bytes));
        byte[] bcd8 = getCurrentTimeBCD8();
        System.out.println(Arrays.toString(bcd8));
        byte[] bytes1 = str2Bcd(getCurrentTimeGTM8(DATETIME_FORMAT_SIMPLE));
        System.out.println(Arrays.toString(bytes1));
    }
}
